// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var topMenu = require("../topMenu/topMenu");
	var router = require("../hash/pageRouter");
	var footer = require("../footer/footer_main");
	var dialog = require("dialog");
	var databind = require("databind");
	var validate = require("validate");
	var html_html_bulkstockWarning = require("./html_bulkstockWarning.html");
	var storage = window.sessionStorage;
	var ftv;
	var pageIndex = 1,
		pageSize = 6,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化
	var imgSrc;
	var init = function(number) {
		ftv = storage.getItem("firstTimeVisit");

		footer.init();
		if (number == 0 || number == null || number == undefined) {
			number = 0;
		}
		topMenu.init(number);
		$("#footer").removeClass("none");
		$(".table-section").removeClass("none");
		$(".footer").removeClass("none");
		if ($("#mainPages").css("display") == "none") {
			databind.fillInput1($("#mainPages"), {
				action: "personalWorkbench.json",
				async: false
			}, function() {
				inquireCurrentOperationList();
			});
		} else {
			databind.fillInput1($("#mainPages"), {
				action: "personalWorkbench.json?flag=true",
				async: false
			}, function() {
				inquireCurrentOperationList();
			});
		}

		inquireEnterprisePageList();
		initEvent();
		router.init();

	};
	
	var initEvent = function() {

		//1分钟更新一次工作台数据
		setInterval(function() {
			if ($("#mainPages").css("display") == "none") {
				databind.fillInput($("#mainPages"), {
					action: "personalWorkbench.json",
					async: false
				});
			} else {
				databind.fillInput($("#mainPages"), {
					action: "personalWorkbench.json?flag=true",
					async: false
				});
			}
			inquireEnterprisePageList();
		}, 3 * 60 * 1000);

		//若未登录，则返回登陆页面
		if (base.getData("sid") == null || base.getData("sid") == "" || base.getData("sid") == undefined) {
			top.location.href = "../login/login.html";
		}

		//点击初审
		$(".financeUnexam").unbind("click").click(function() {
			router.route("main/main_finance.html?left=0&financeState=1", true);
		});
		
		//点击通知锁定
		$(".noticeReceive").unbind("click").click(function() {
			router.route("main/main_finance.html?left=2&financeState=5", true);
		});
		
		//点击协议上传
		$(".contractUnsign").unbind("click").click(function() {
			router.route("main/main_finance.html?left=1&financeState=3&contractType=1", true);
		});
		
		//点击监管确认
		$(".financeUnsupervise").unbind("click").click(function() {
			router.route("main/supervisionNotice_list.html?left=3", true);
		});
		
		//点击合同上传
		$(".contractUnsign1").unbind("click").click(function() {
			router.route("main/main_finance.html?left=4&financeState=3&contractType=2", true);
		});
		
		//点击合同签署
		$(".contractUnsign2").unbind("click").click(function() {
			router.route("main/main_finance.html?left=5&financeState=3&contractType=3", true);
		});
		
		//点击政审
		$(".financeUnexam1").unbind("click").click(function() {
			router.route("main/main_finance.html?left=6&financeState=23", true);
		});
		
		//点击放款申请
		$(".financeUnrelease").unbind("click").click(function() {
			router.route("main/loanNotice_list.html?left=7", true);
		});
		
		//点击放款审核
		$(".financeUnloan").unbind("click").click(function() {
			router.route("main/loanNoticeList.html");
		});
		
		//点击增补合同
		$(".supplementContract").unbind("click").click(function() {
			router.route("main/main_finance.html?left=9&financeState=31&contractType=4", true);
		});
		
		//点击通知运输
		$(".financeUntransport").unbind("click").click(function() {
			router.route("main/transportNotice_list.html?left=10", true);
		});

		//点击提货审批
		$(".financePickExam").unbind("click").click(function() {
			router.route("main/pickupList.html?left=11", true);
		});
		
		//点击收款确认
		$(".financeUnreceive").unbind("click").click(function() {
			router.route("main/receivablesNotice_list.html?left=12", true);
		});
		
		//点击收款复核
		$(".receiptConfirm").unbind("click").click(function() {
			location.href = "../moneyStatementManage/receiptConfirmManage.html";
		});
		
		//点击放货通知
		$(".financeUnreceiveBulkstock").unbind("click").click(function() {
			router.route("main/deliveryNotice_list.html?left=13", true);
		});

		//点击解除监管
		$(".cancelSupervise").unbind("click").click(function() {
			router.route("main/supervisionRelieveList.html?left=14", true);
		});
		
		//-------------------------------------------

		//点击提前还款
		$(".advanceRefund").unbind("click").click(function() {
			router.route("main/pickupPreList.html?left=11", true);
		});
		
		//点击延期还款
		$(".delayRefund").unbind("click").click(function() {
			router.route("main/pickupDelayList.html?left=12", true);
		});

		//点击延期计息
		$(".interestDelay").unbind("click").click(function() {
			router.route("main/interestDelayList.html?left=13", true);
		});

		//点击扣货申请
		$(".deductsApply").unbind("click").click(function() {
			router.route("main/detainedList.html?left=14", true);
		});
		
		//点击赎货申请
		$(".redeemApply").unbind("click").click(function() {
			router.route("main/pickupRansomList.html?left=15", true);
		});

		//点击扣货申请
		$(".badBulkstock").unbind("click").click(function() {
			router.route("main/badassetsHandleList.html?left=16", true);
		});

		//点击异常管理
		$(".abnormalManage").unbind("click").click(function() {
			location.href = "../exception/exceptionManage.html";
		});
		
		//点击补货审核
		$(".replenishAudit").unbind("click").click(function() {
			location.href = "../financing/replenishAudit.html";
		});
		
		//点击补货确认
		$(".replenishConfirm").unbind("click").click(function() {
			location.href = "../financing/replenishConfirm.html";
		});
		
		//点击追加保证金审核
		$(".bailAdditionAudit").unbind("click").click(function() {
			location.href = "../financing/bailAdditionaudit.html";
		});
		
		//点击退还保证金审核
		$(".bailReturnAudit").unbind("click").click(function() {
			location.href = "../financing/bailReturnAudit.html";
		});
		
		//点击融资预警
		$(".schemeResult").unbind("click").click(function() {
			location.href = "../auditWarning/warningCenter.html#auditWarning/warningSchemeResultList";
		});
		
		
		//点击合同预警
		$(".riskWarning").unbind("click").click(function() {
			location.href = "../auditWarning/warningCenter.html#auditWarning/warningSchemeResultList1";
		});
		
		
//		//点击风险预警
//		$(".riskWarning").unbind("click").click(function() {
//			location.href = "../auditWarning/warningCenter.html#auditWarning/riskWarning";
//		});
		
		//点击保险审核
		$(".insuranceAudit").unbind("click").click(function(){
			location.href = "../insuranceManage/insuranceManage.html?insuranceState=1";
		});
		
		//开卡审核
		$(".userCardAudit").unbind("click").click(function(){
			location.href = "../client/clientManage.html?leftTime=60";
		});
		
		//用户审核
		$(".enterpriseAudit").unbind("click").click(function(){
			location.href = "../client/clientManage.html?auditState=1";
		});
		
		//分销商审核
		$(".distributorAudit").unbind("click").click(function(){
			location.href = "../client/distributors.html?auditState=1";
		});
		
//		//佣金提取审核
//		$(".commissionExtract").unbind("click").click(function(){
//			location.href = "../financing/commissionExtractAudit.html";
//		});

		//待放款
		$(".waitLoan").unbind("click").click(function(){
			location.href = "../moneyStatementManage/receiptManage.html?verifiCancelState=false";
		});
		
		//待收款
		$(".waitReceipt").unbind("click").click(function(){
			location.href = "../moneyStatementManage/disbursementManage.html?verifiCancelState=false";
		});
		
		//-------------------------------
		//仓储类型提货列表-提货申请
		$(".warehousePickupUnsubmit").unbind("click").click(function(){
//			router.route("warehousePickup/wareHousePickupList.html?pickupState=0");
			location.href="../warehousePickup/warehousePickup.html?pickupState=0";
		});
		
		//仓储类型提货列表-提货初审
		$(".warehousePickupFirstApprove").unbind("click").click(function(){
//			router.route("warehousePickup/wareHousePickupList.html?pickupState=2");
			location.href="../warehousePickup/warehousePickup.html?pickupState=2";
		});
		
		//仓储类型提货列表-合同上传
		$(".warehousePickupUpload").unbind("click").click(function(){
//			router.route("warehousePickup/wareHousePickupList.html?pickupState=[3, 4, 7, 8]");
			location.href="../warehousePickup/warehousePickup.html?pickupState=[3, 4, 7, 8]";
		});
		
		//仓储类型提货列表-合同签署
		$(".warehousePickupSign").unbind("click").click(function(){
//			router.route("warehousePickup/wareHousePickupList.html?pickupState=6");
			location.href="../warehousePickup/warehousePickup.html?pickupState=6";
		});
		
		
		//仓储类型提货列表-提货政审
		$(".warehousePickupSecondApprove").unbind("click").click(function(){
//			router.route("warehousePickup/wareHousePickupList.html?pickupState=10");
			location.href="../warehousePickup/warehousePickup.html?pickupState=10";
		});
		
		//仓储类型提货列表-提货处理
		$(".warehousePickupRelease").unbind("click").click(function(){
//			router.route("warehousePickup/wareHousePickupList.html?pickupState=[11, 12]");
			location.href="../warehousePickup/warehousePickup.html?pickupState=[11]";
		});
	};

	//权限控制
	var inquireCurrentOperationList = function() {
		base.fnAjax({
			action: 'inquireCurrentOperationList.json',
			async: false,
			fnSuccess: function(data) {
				if (data.status == 200) {
					for (var i = 0; i < data.datas.length; i++) {
						var permission = data.datas[i];
						var permissionId = permission.permissionId;
						$.each($("#mainPages").find("div"), function() {
							var $this = $(this);
							if ($this.attr("permissionId") == permissionId) {
								$this.removeClass("disabled");
							}
							if ("priceWarning_1" == permissionId) {
								if (ftv == null) {
									inquireBulkstockWarningPageList();
									storage.setItem("firstTimeVisit", "false");
									ftv = "false";
								}
							}
						});
					}
				} else {
					base.alert(3, data.message);
				}
			}
		});
	};
	
	//权限控制
	var inquireEnterprisePageList = function() {
		base.fnAjax({
			action: 'inquireEnterprisePageList.json',
			async: false,
			oData: {
				leftTime: 60
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					$("span[name=userCardAudit]").html(data.totalRecordCount);
				} else {
					base.alert(3, data.message);
				}
			}
		});
	};

	//查询大宗商品价格波动预警
	var inquireBulkstockWarningPageList = function() {
		base.fnAjax({
			action: "inquireWarningSchemeResultPageList.json",
			oData: {
				pageIndex: pageIndex,
				pageSize: pageSize,
				factor: 0
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					if (data.datas.length > 0) {
						$("#popdialog").html(html_html_bulkstockWarning);
						var tpl = $("#bulkstockWarningTpl").html();
						var html = juicer(tpl, data);
						$("#bulkstockWarningTable").html(html);
						dialog.showDialog($("#bulkstockWarning_dialog"));
						//确定
						$("#bulkstockWarning_dialog .sure_yes").unbind("click").click(function() {
							dialog.hideDialog($("#bulkstockWarning_dialog"));
						});
						
						//详情
						$("#bulkstockWarning_dialog .sure_detail").unbind("click").click(function() {
							location.href = "/finance/html/platform/auditWarning/warningCenter.html#auditWarning/warningSchemeResultList";
						});

						riseAndFallColor();

						totalPageCount = data.totalPageCount;
						totalRecordCount = data.totalRecordCount;
						base.pageControl(
							$("#bulkstockWarning_dialog .pagerCon"),
							totalRecordCount,
							data.pageIndex,
							data.pageSize,
							function(p) {
								pageIndex = p;
								inquireBulkstockWarningPageList();
							});
					}
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	}

	//改变涨跌颜色
	var riseAndFallColor = function() {
		var riseAndFall = $(".riseAndFall");
		$.each(riseAndFall, function() {
			var _this = $(this);
			if ($.trim(_this.html()).charAt(0) == "-") {
				_this.css("color", "#33A55E");
			} else if ($.trim(_this.html()).charAt(0) == "+") {
				_this.css("color", "#F1592A");
			} else {}
		});
	};

	return {
		init: init
	};
});